package com.happyhell.hiyou.rest.interceptors;

import java.util.Map;

import com.happyhell.hiyou.model.UserInfo;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ValidationAwareSupport;
import com.opensymphony.xwork2.interceptor.Interceptor;
 
public class AuthenticationInterceptor implements Interceptor {
 
    private static final long serialVersionUID = -5011962009065225959L;
    public static final String USER_SESSION_KEY = "hiyou.session.user";
    @Override
    public void destroy() {
        //release resources here
    }
 
    @Override
    public void init() {
        // create resources here
    }
 
    @Override
    public String intercept(ActionInvocation actionInvocation)
            throws Exception {
        System.out.println("inside auth interceptor");
        Map<String, Object> sessionAttributes = actionInvocation.getInvocationContext().getSession();
        UserInfo user = (UserInfo) sessionAttributes.get("USER");
         
        ValidationAwareSupport action = (ValidationAwareSupport) actionInvocation.getAction();
        if(action instanceof UserAware){
        	((UserAware) action).setUserInfo(user);
        }
        if(user == null){
        	action.addActionError("not login");
        	return actionInvocation.invoke();
        }else{
            return actionInvocation.invoke();
        }
    }
 
}
